<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        box-sizing: border-box;
      }
      div {
        float: left;
        width: 100px;
        height: 100px;
        border: solid 1px black;
        margin-right: 10px;
      }
    </style>
  </head>
  <body>
    <h1>点击切换颜色</h1>
    <div></div>
    <div></div>
    <div></div>
    <script>
      let items = document.querySelectorAll("div");
      let flag = 0;
      for (let i = 0; i < items.length; i++) {
        items[i].addEventListener("click", function () {
          if (flag == 0) {
            items[i].style.backgroundColor = "pink";
            flag = 1;
          } else {
            items[i].style.backgroundColor = "";
            flag = 0;
          }
        });
      }
    </script>
  </body>
</html>
